//
//  MeatMarketApi.m
//  PigMarket
//
//  Created by tunji on 2019/6/13.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "MeatMarketApi.h"
#import "Request.h"
#import "RequestHandler.h"
#import "NetworkConfigration.h"
#import "SystemUtil.h"


//肉市-同城掌柜
#define URLSameCityListBind @"/shopIndex/nearbyManager"
@implementation MeatMarketApi

#pragma mark - 肉市-买肉-广告
- (RACSignal<BuyMeatadvertiseListModel *> *)getBuyMeatAdvertiseListData{

    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLBuyMeatAdvertiseBind];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeNone;
    request.responseType = [BuyMeatadvertiseListModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 肉市-买肉-部位列表
- (RACSignal<BuyMeatPartListModel *> *)getBuyMeatPartListDataWithType:(NSNumber *)type{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLBuyMeatPartBind];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    request.param = @{@"type":type};
    request.responseType = [BuyMeatPartListModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 肉市-买肉-店铺列表
- (RACSignal<BuyMeatListModel *> *)getShoppingListByParams:(MeatShopListParam *)params{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLBuyMeatListBind];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    request.param = params;
    request.responseType = [BuyMeatListModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 肉市-同城商铺
- (RACSignal<SameCityListModel *> *)getSameCityListDataWithMemberId:(NSNumber *)memberId Page:(NSNumber *)page Limit:(NSNumber *)limit Lon:(NSString *)lon Lat:(NSString *)lat{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLSameCityListBind];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId,@"page":page,@"limit":limit,@"lon":lon,@"lat":lat};
    request.responseType = [SameCityListModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 肉市-获取店铺信息
- (RACSignal<BuyMeatShopDetailModel *> *)getBuyMeatShoppDetailByShopId:(NSNumber *)shopId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLBuyMeatShopDetail];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    request.param = @{@"memberId":kMemberId,@"shopId":shopId};
    request.responseType = [BuyMeatShopDetailModel class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 售卖中的猪-转让中的猪
- (RACSignal<PigTypeListModel *> *)getBuyMeatPigTypeListByShopId:(NSNumber *)shopId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLBuyMeatPigTypeList];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeNone;
    //request.param = @{@"memberId":kMemberId,@"shopId":shopId , @"pigPositionId":pigPositionId};
    request.param = @{@"memberId":kMemberId,@"shopId":shopId };

    request.responseType = [PigTypeListModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 点赞/取消点赞店铺
- (RACSignal <NSNull *> *)getBuyMeatShopLikeByShopId:(NSNumber *)shopId isLike:(NSNumber *)isLike{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@/%@", URLBuyMeatShopLike, shopId, isLike, kMemberId];
    
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}

#pragma mark - 收藏/取消收藏 店铺
- (RACSignal <NSNull *> *)getBuyMeatShopCollectionByShopId:(NSNumber *)shopId isCollection:(NSNumber *)isCollection{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLBuyMeatShopCollection];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    if (isCollection) {
        request.param = @{@"memberId":kMemberId,@"shopId":shopId, @"type":isCollection};
    }
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 店铺中的购物车
- (RACSignal<PigTypeModel *> *)getBuyMeatCartListByShopId:(NSNumber *)shopId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLBuyMeatShopCartList];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":kMemberId,@"shopId":shopId };
    
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 我的购物车
- (RACSignal<MeatCartModel *> *)getMyMeatCartListByPage:(NSNumber *)page{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMyMeatShopCartList];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":kMemberId,@"page":page, @"limit":@10};
    request.responseType = [MeatCartModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 加入购物车
- (RACSignal <NSNull *> *)getMeatAddCartByShopId:(NSNumber *)shopId advanceSaleId:(NSNumber *)advanceSaleId{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@/%@/%@", URLMeatAddCart, kMemberId, advanceSaleId, shopId];
    
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 删除购物车
- (RACSignal<NSNull *> *)getMyMeatDeleteCartByIds:(NSArray *)Ids{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatDeleteCart];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"list":Ids};
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 从分割预售中的猪进入
- (RACSignal<MeatPayOrderModel *> *)getMeatPayTypeByShopId:(NSNumber *)shopId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatPayTypeSale];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":kMemberId,@"shopId":shopId};
    
    request.responseType = [MeatPayOrderModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 从整猪预售中的猪进入
- (RACSignal <NSNull *> *)getMeatPayWholeByAdvanceSaleId:(NSNumber *)advanceSaleId{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLMeatPayWholeSale, advanceSaleId];
    
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 下单支付成功之后调用---此接口
- (RACSignal <NSNull *> *)getMeatPayByOrderId:(NSNumber *)orderId{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLPayOrder, orderId];
    
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}

#pragma mark - 从分割预售中的猪进入
//支付方式:1余额支付;2支付宝支付;3微信支付
- (RACSignal<PigTypeModel *> *)getMeatPayByOrderId:(NSNumber *)orderId payAmount:(NSNumber *)payAmount  payType:(NSString *)payType{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLPayFinish];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"orderId":orderId, @"payAmount":payAmount, @"payType":payType};
    
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 支付成功之后查看订单
- (RACSignal <NSNull *> *)getMeatPayOrderDetailByOrderId:(NSNumber *)orderId{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLPayOrderDetails, orderId];
    
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 历史评论(查看店铺评论)
- (RACSignal<PigTypeModel *> *)getMeatShopCommentsTypeByShopId:(NSNumber *)shopId page:(NSNumber *)page{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatShopComments];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"shopId":shopId,@"page":page, @"limit":@10};

    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}



#pragma mark - 上报内容
- (RACSignal<PigTypeModel *> *)getMeatShopComplaintsByParams:(MeatShopComplaintsParam *)params{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatShopComplaints];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = params;
    request.responseType = [PigTypeModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 评论页面显示小猪编号
- (RACSignal <NSNull *> *)getMeatPigNumberByShopId:(NSNumber *)shopId{
    
    Request *request = [[Request alloc] init];
    NSString *url = [NSString stringWithFormat:@"%@/%@", URLMeatPigNumber, shopId];
    
    request.URLString = [self getFullURLString:url];
    request.method = kHTTPMethodGET;
    request.oauthType = OAuthTypeMember;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 下单校验接口
- (RACSignal<MeatPayModel *> *)getMeatPayOrderCheckByParams:(MeatOrderCheckParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatPayOrderCheck];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [MeatPayModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 提交订单（下单)
- (RACSignal<MeatOrderModel *> *)getMeatPayOrderSumbitByParams:(MeatOrderSumbitParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatPayOrderSumbit];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [MeatOrderModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 获取价格区间列表
- (RACSignal<MeatPriceRangeModel *> *)getMeatShopPriceList{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatPriceList];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.responseType = [MeatPriceRangeModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 打烊店铺留言
- (RACSignal<NSNull *> *)getMeatShopMessageByParams:(MessageParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatShopCloseMessage];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = nil;
    return [self createSignalWithReuqest:request];
}


#pragma mark - 物价局
- (RACSignal<MeatPriceModel *> *)getMeatPriceReferenceByTribeId:(NSNumber *)tribeId page:(NSNumber *)page{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatShopPriceReference];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"tribeId":tribeId,@"page":page,@"limit":@10};
    request.responseType = [MeatPriceModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 物价局搜索
- (RACSignal<PigTypeModel *> *)getMeatPriceSearchByTribeStr:(NSString *)tribeStr{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatShopPriceSearch];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"tribeStr":tribeStr};
    request.responseType = [MeatOrderSumbitParam class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 筛选页查部落店铺列表 排序
- (RACSignal<MeatShopSortModel *> *)getMeatShopSortingByParams:(MeatShopSortingParam *)parmas{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatShopSorting];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = parmas;
    request.responseType = [MeatShopSortModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 获取我的店铺列表 同城掌柜的
- (RACSignal<PigTypeModel *> *)getMeatShopCityListByMemberId:(NSString *)memberId{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatShopCityList];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = @{@"memberId":memberId};
    request.responseType = [MeatOrderSumbitParam class];
    return [self createSignalWithReuqest:request];
}

#pragma mark - 同城掌柜
- (RACSignal<MeatShopNearByModel *> *)getMeatShopNearByListByParam:(MeatShopNearByParam *)params{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatShopNearbyManager];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.param = params;
    request.responseType = [MeatShopNearByModel class];
    return [self createSignalWithReuqest:request];
}


#pragma mark - 获取部落范围
- (RACSignal<PigTypeModel *> *)getMeatShopTriberRange{
    
    Request *request = [[Request alloc] init];
    request.URLString = [self getFullURLString:URLMeatTriberRange];
    request.method = kHTTPMethodPOST;
    request.oauthType = OAuthTypeMember;
    request.responseType = [MeatOrderSumbitParam class];
    return [self createSignalWithReuqest:request];
}




- (NSString *)getFullURLString:(NSString *)URLString {
    
    NetworkConfigration *configration = [NetworkConfigration defaultNetworkConfiguration];
    return [NSString stringWithFormat:@"%@%@", configration.baseApiURL, URLString];
}


- (RACSignal *)createSignalWithReuqest:(Request *) request {
    
    return [[RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        
        RequestHandler *handler = [[RequestHandler alloc] initWithRequest:request responseCallback:^(Response *response) {
            
            
            if (response.code == ResultCodeSuccess) {
                
                if(request.responseType) {
                    [subscriber sendNext:response.data];
                } else {
                    [subscriber sendNext:[NSNull null]];
                }
                [subscriber sendCompleted];
                
            } else {
                if (response.code == ResultCodeDeviceChanged) {//更换设备
                    UserInfo *userInfo = [UserInfoDataProvider sharedInstance].userInfo;
                    userInfo.isLogin = NO;
                    [UserInfoDataProvider sharedInstance].userInfo = userInfo;
                }
                NSError *error = [NSError errorWithDomain:@"com.naturalgrowth.pigmarket.ErrorDomain" code:response.code userInfo: @{ NSLocalizedDescriptionKey : response.msg }];
                [subscriber sendError:error];
            }
            
        }];
        
        [handler sendRequest];
        
        return [RACDisposable disposableWithBlock:^{
            
            
            [handler cancelRequest];
        }];
        
    }] subscribeOn:[RACScheduler scheduler]] ;
}

@end
